package com.spring.sunflower.util;

import android.app.Service;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;

import androidx.annotation.RequiresApi;

import com.spring.sunflower.R;

import java.security.Provider;

public class VibrateUtils {
    private static VibrateUtils vibrateUtils;

    private static Context mContext;
    private static Vibrator vibrator;

    public static VibrateUtils getInstance(Context context) {
        mContext = context;
        vibrator = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE);
        if (vibrateUtils == null)
            synchronized (VibrateUtils.class) {
                if (vibrateUtils == null) {
                    vibrateUtils = new VibrateUtils();
                }
            }
        return vibrateUtils;
    }

    public void playVibrateWithMessageNotification() {
        long[] pattern = {50, 200, 100, 200};
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM) //key
                    .build();
            vibrator.vibrate(pattern, -1, audioAttributes);
        } else {
            vibrator.vibrate(pattern, -1);
        }
    }

    public void playVibrateClick(int vibrationEffect) {
        vibrator.vibrate(vibrationEffect);
    }

    public void cancelVibrate() {
        if (vibrator != null) {
            vibrator.cancel();
        }
    }
}